//模拟实现生产者消费模型
public class MyBlockingQueue {

    private int[] array = null;

    public MyBlockingQueue() {
        array = new int[1000];
    }

    //计数
   volatile int size = 0;
    //队头
    int begin = 0;
    //队尾
    int end = 0;


    public void put(int n) throws InterruptedException {
        synchronized(this) {
            //二次确定
            while (size == array.length) {
                //满了就阻塞
                this.wait();
            }

            array[begin] = n;
            begin = (begin + 1) % array.length;
            size++;
            //到这说明预计添加成功了队列不再空   唤醒继续take
            this.notify();
        }
    }

    public int take() throws InterruptedException {

        synchronized (this) {
            //二次确定
            while (size == 0) {
                //空了就阻塞
                this.wait();
            }

            int val = array[end];
            end = (end + 1) % array.length;
            size--;

            //执行到这说明元素不是满的了 所以唤醒继续put
            this.notify();
            return val;
        }
    }

}
